package com.xianniu.ltc.model.po;

import com.alibaba.fastjson.JSONObject;
import com.rebuild.core.service.notification.Message;
import com.rebuild.core.service.notification.MessageBuilder;
import lombok.Data;
import org.apache.commons.beanutils.BeanUtils;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;

/**
 * 消息通知新增PO类
 * @author dingyuxin
 */
@Data
public class NotificationAddPO implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 企业id
     */
    private Integer tenantId;
    /**
     * 企业编码
     */
    private String tenantCode;
    /**
     * 发送人
     */
    private String fromUser;

    /**
     * 接收人
     */
    private String toUser;

    /**
     * 内容
     */
    private String message;

    /**
     * 消息分类
     */
    private Integer msgType;

    /**
     * 相关业务记录
     */
    private String relatedRecord;

    /**
     * 项目编号
     */
    private String projectCode;

    /**
     * 备注
     */
    private String entityName;

    /**
     * 这一条的数据名
     */
    private String dataValue;

    /**
     * 跳转目标类型0h5 1app
     */
    private Integer targetType;

    private LocalDateTime publishDate;

    public static NotificationAddPO builder(JSONObject message, String tenantCode, String fromUser, String entityName, String toUser, String projectCode, String dataValue){
        String formatMsg = MessageBuilder.formatMessage(message.getString("message"), false, false);
        NotificationAddPO addPO = JSONObject.parseObject(message.toJSONString(), NotificationAddPO.class);
        addPO.setTenantCode(tenantCode);
        addPO.setEntityName(entityName);
        addPO.setTargetType(0);
        addPO.setFromUser(fromUser);
        addPO.setMsgType(Message.TYPE_APPROVAL);
        addPO.setToUser(toUser);
        addPO.setPublishDate(LocalDateTime.now());
        addPO.setMessage(formatMsg);
        addPO.setProjectCode(projectCode);
        addPO.setDataValue(dataValue);
        return addPO;
    }
}

